/**
  ******************************************************************************
  * File Name          : USART.c
  * Description        : This file provides code for the configuration
  *                      of the USART instances.
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2018 STMicroelectronics
  *
  * Redistribution and use in source and binary forms, with or without modification,
  * are permitted provided that the following conditions are met:
  *   1. Redistributions of source code must retain the above copyright notice,
  *      this list of conditions and the following disclaimer.
  *   2. Redistributions in binary form must reproduce the above copyright notice,
  *      this list of conditions and the following disclaimer in the documentation
  *      and/or other materials provided with the distribution.
  *   3. Neither the name of STMicroelectronics nor the names of its contributors
  *      may be used to endorse or promote products derived from this software
  *      without specific prior written permission.
  *
  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  *
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "usart.h"
/* USER CODE BEGIN 0 */
#include "board_set.h"
#include "los_hwi.h"
#include "bsp_api.h"
#include "app_common.h"
#include "omip_log.h"
/* USER CODE END 0 */

#define BUFFER_SIZE 100
#define DMA_BUFF_LEN  	255 //DMA环型队列缓冲区大小
#define DMA_CIR_IDX(a) ((a)>=DMA_BUFF_LEN?((a)%DMA_BUFF_LEN):(a)) //DMA环型队列索引计算

typedef struct {
    unsigned char 	data_buff[DMA_BUFF_LEN];
    unsigned short 	data_head;
    unsigned short 	data_tail;
    unsigned int 	data_len;
}DMA_BUFF;

UART_HandleTypeDef	huart1;
UART_HandleTypeDef	huart3;
UART_HandleTypeDef	huart5;


DMA_HandleTypeDef hdma_usart1_tx, hdma_usart1_rx;
uint8_t i_usart1_rx_buffer[BUFFER_SIZE];

DMA_HandleTypeDef hdma_usart3_rx, hdma_usart3_tx;
uint8_t i_usart3_rx_buffer[BUFFER_SIZE];

static DMA_BUFF dma_uart1, dma_uart3; //uart1环型缓冲区

extern HAL_StatusTypeDef usart1_senddata(uint8_t *data,uint32_t len);

int uart_send_data(USART_TypeDef *pUart,unsigned char*pData ,unsigned short iLen,unsigned int iTmOut) //串口发送数据
{
	int iState =0;
	if(pUart ==USART1)	{
		iState = usart1_senddata(pData, iLen);
	}
	else {
		return -1;
	}
	return iState;
}

int check_uart_data(USART_TypeDef *pUart) //检测串口DMA缓冲中的数据(不会改变DMA缓冲)
{
	DMA_BUFF *pDmaBuff =NULL;
	if(pUart ==USART1)	{
		pDmaBuff =&dma_uart1;
	}
	else if(pUart ==USART3)	{
		pDmaBuff =&dma_uart3;
	}
	else {
		return -1;
	}
	return pDmaBuff->data_len;
}

int recv_uart_data(USART_TypeDef *pUart,unsigned char *pRecvBuff,unsigned short iRecvBytes) //读取串口DMA缓冲中的数据
{
	int iRet =0;
	DMA_BUFF *pDmaBuff =NULL;
	if(pUart ==USART1)	{
		pDmaBuff =&dma_uart1;
	}
	else if(pUart ==USART3)	{
		pDmaBuff =&dma_uart3;
	}
	else {		return -1;
	}
    if(pDmaBuff->data_len <=0 ||iRecvBytes<=0) return 0;
    if(iRecvBytes <= pDmaBuff->data_len) //有足够长度
    {
    	for(int i=0; i<iRecvBytes; i++)
		{
			if(pRecvBuff) pRecvBuff[i] =pDmaBuff->data_buff[pDmaBuff->data_head];
			pDmaBuff->data_len--;
			pDmaBuff->data_head = DMA_CIR_IDX(pDmaBuff->data_head +1);
		}
		iRet =iRecvBytes;
    }
    else
    {
		iRet =0;
    }
    return iRet;
}

int clear_uart_data(USART_TypeDef *pUart) //清除串口DMA缓冲中的数据
{
	if(pUart == USART1) {
		memset(&dma_uart1,0,sizeof(dma_uart1));
	}
	else if(pUart == USART3) {
		memset(&dma_uart3, 0, sizeof(dma_uart3));
	}
	else {
		return -1;
	}
	return 0;
}

//void DMA2_Stream2_IRQHandler(void)
static void DMA2_Stream5_irq(void)
{
    HAL_DMA_IRQHandler(&hdma_usart1_rx);
}

static void DMA2_Stream7_irq(void)
{
    HAL_DMA_IRQHandler(&hdma_usart1_tx);
}

HAL_StatusTypeDef usart1_senddata(uint8_t *data,uint32_t len)
{
    while(0 != __HAL_DMA_GET_COUNTER(&hdma_usart1_tx))  /* 等待发送完成 */
    {
    }
    return HAL_UART_Transmit_DMA(&huart1,data,len);
}

void uart1_irq(void) //UART1接收中断响应函数
{
    uint32_t tmp_flag = 0;
    uint32_t temp, i;
    static uint8_t s_led_rs485 = LED_OPEN;
	DMA_BUFF *pDmaBuff =&dma_uart1;
    HAL_UART_IRQHandler(&huart1);
    if(USART1 == huart1.Instance)
    {
        tmp_flag = __HAL_UART_GET_FLAG(&huart1, UART_FLAG_IDLE); //获取IDLE标志位            
        if((tmp_flag != RESET))//idle标志被置位
        { 
            __HAL_UART_CLEAR_IDLEFLAG(&huart1);//清除标志位                     
            HAL_UART_DMAStop(&huart1); //     
            temp =  BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart1_rx); //总计数减去未传输的数据个数，得到已经接收的数据个数
			for(i = 0; i < temp; i++)
			{
				if(DMA_CIR_IDX(pDmaBuff->data_tail +1) != pDmaBuff->data_head) //加入数据到缓冲,如果缓冲区满了丢弃数据
				{
					pDmaBuff->data_buff[pDmaBuff->data_tail] = i_usart1_rx_buffer[i];
					pDmaBuff->data_len++;
					pDmaBuff->data_tail = DMA_CIR_IDX(pDmaBuff->data_tail+1);
				}
				
			}
			
            HAL_UART_Receive_DMA(&huart1, i_usart1_rx_buffer, BUFFER_SIZE);//重新打开DMA接收
            LED_RS485_2_(s_led_rs485);
			s_led_rs485 = !s_led_rs485;
        }
    }

}

void USART1_UART_Init(void) //USART1 init function
{
	UINT32 ret = 0;
    memset(&dma_uart1,0,sizeof(dma_uart1));

	//MX_DMA_Init();
	__HAL_RCC_DMA2_CLK_ENABLE();
	ret |= LOS_HwiCreate(DMA2_Stream5_IRQn, 0, 0, DMA2_Stream5_irq, 0);
	ret |= LOS_HwiCreate(DMA2_Stream7_IRQn, 0, 0, DMA2_Stream7_irq, 0);

    huart1.Instance = USART1;
    huart1.Init.BaudRate = 4800;
    huart1.Init.WordLength = UART_WORDLENGTH_9B;
    huart1.Init.StopBits = UART_STOPBITS_1;
    huart1.Init.Parity = UART_PARITY_ODD;
    huart1.Init.Mode = UART_MODE_TX_RX;
    huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart1.Init.OverSampling = UART_OVERSAMPLING_16;
    if(HAL_UART_Init(&huart1) != HAL_OK) _Error_Handler(__FILE__, __LINE__);
    ret |= LOS_HwiCreate(USART1_IRQn, 3, 0, uart1_irq, 0); //create a hardware interrupt to deal ble_uart_irq

    __HAL_UART_ENABLE_IT(&huart1, UART_IT_IDLE);//使能IDLE中断
    ret |= HAL_UART_Receive_DMA(&huart1, i_usart1_rx_buffer, BUFFER_SIZE);
	print_debug(0, "usart1 init, ret = %d", ret);

}

__attribute__((used)) int _write(int fd, char *ptr, int len)
{
    (void)HAL_UART_Transmit(&huart5, (uint8_t *)ptr, len, 0xFFFF);
    return len;
}

void MX_UART5_Init(void) //UART5 init function
{
	huart5.Instance = UART5;
	huart5.Init.BaudRate = 115200;
	huart5.Init.WordLength = UART_WORDLENGTH_8B;
	huart5.Init.StopBits = UART_STOPBITS_1;
	huart5.Init.Parity = UART_PARITY_NONE;
	huart5.Init.Mode = UART_MODE_TX_RX;
	huart5.Init.HwFlowCtl = UART_HWCONTROL_NONE;
	huart5.Init.OverSampling = UART_OVERSAMPLING_16;
	if (HAL_UART_Init(&huart5) != HAL_OK) Error_Handler();
}

/*
*********************************************************************************************************
*    函 数 名:DMA1_Stream1_IRQHandler
*    功能说明: 串口DMA接收中断函数
*    形    参: 无
*    返 回 值: 无
*********************************************************************************************************
*/
//void DMA1_Stream1_IRQHandler(void)
static void DMA1_Stream1_irq(void)
{
    HAL_DMA_IRQHandler(&hdma_usart3_rx);
}

/*
*********************************************************************************************************
*    函 数 名:DMA1_Stream3_IRQHandler
*    功能说明: 串口DMA发送中断函数
*    形    参: 无
*    返 回 值: 无
*********************************************************************************************************
*/
//void DMA1_Stream3_IRQHandler(void)
static void DMA1_Stream3_irq(void)
{
    HAL_DMA_IRQHandler(&hdma_usart3_tx);
}

static void usart3_irq(void)    //串口中断
{
    uint32_t tmp_flag = 0;
    uint32_t temp,i;
    static uint8_t s_led_rs485 = LED_OPEN;
	DMA_BUFF *pDmaBuff =&dma_uart3;
    HAL_UART_IRQHandler(&huart3);
    if(USART3 == huart3.Instance)
    {
        tmp_flag =__HAL_UART_GET_FLAG(&huart3,UART_FLAG_IDLE); //获取IDLE标志位            
        if((tmp_flag != RESET))//idle标志被置位
        { 
          
            __HAL_UART_CLEAR_IDLEFLAG(&huart3);//清除标志位                     
            HAL_UART_DMAStop(&huart3); //     
            //temp  =  __HAL_DMA_GET_COUNTER(&hdma_usart1_rx);// 获取DMA中未传输的数据个数                     
            temp =  BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&hdma_usart3_rx); //总计数减去未传输的数据个数，得到已经接收的数据个数
			for(i = 0; i < temp; i++)
			{
				if(DMA_CIR_IDX(pDmaBuff->data_tail +1) != pDmaBuff->data_head) //加入数据到缓冲,如果缓冲区满了丢弃数据
				{
					pDmaBuff->data_buff[pDmaBuff->data_tail] = i_usart3_rx_buffer[i];
					pDmaBuff->data_len++;
					pDmaBuff->data_tail = DMA_CIR_IDX(pDmaBuff->data_tail+1);
				}
				
			}
			/*
			memcpy(g_usart3_rx_buffer,i_usart3_rx_buffer,temp);
            g_usart3_rx_buffer[temp] = '\0';
            for(i=0;i<temp;i++)
                printf("%02x",g_usart3_rx_buffer[i]);
            printf("\r\n");
			*/
			
            HAL_UART_Receive_DMA(&huart3, i_usart3_rx_buffer, BUFFER_SIZE);//重新打开DMA接收
            LED_RS485_1_(s_led_rs485);
			s_led_rs485 = !s_led_rs485;
        }
    }
}

void usart3_senddata(uint8_t *data,uint32_t len)
{
    while(0 != __HAL_DMA_GET_COUNTER(&hdma_usart3_tx))  /* 等待发送完成 */
    {
    }
    HAL_UART_Transmit_DMA(&huart3,data,len);
}

void USART3_UART_Init(void)
{
	UINT32 ret = 0;
	
    memset(&dma_uart3,0,sizeof(dma_uart3));

    //MX_DMA_Init();
	__HAL_RCC_DMA1_CLK_ENABLE();
	ret |= LOS_HwiCreate(DMA1_Stream1_IRQn, 0, 0, DMA1_Stream1_irq, 0);
	ret |= LOS_HwiCreate(DMA1_Stream3_IRQn, 0, 0, DMA1_Stream3_irq, 0);
	
    huart3.Instance = USART3;
    huart3.Init.BaudRate = 4800;
    huart3.Init.WordLength = UART_WORDLENGTH_9B;
    huart3.Init.StopBits = UART_STOPBITS_1;
    huart3.Init.Parity = UART_PARITY_ODD;
    huart3.Init.Mode = UART_MODE_TX_RX;
    huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
    huart3.Init.OverSampling = UART_OVERSAMPLING_16;
    if (HAL_UART_Init(&huart3) != HAL_OK)
    {
        _Error_Handler(__FILE__, __LINE__);
    }
    ret |= LOS_HwiCreate(USART3_IRQn, 3, 0, usart3_irq, 0);
    __HAL_UART_ENABLE_IT(&huart3, UART_IT_IDLE);//使能IDLE中断
    ret |= HAL_UART_Receive_DMA(&huart3,i_usart3_rx_buffer,BUFFER_SIZE);
	__HAL_UART_CLEAR_FLAG(&huart3,UART_FLAG_TC);
	__HAL_UART_ENABLE_IT(&huart3,UART_IT_ERR);
	__HAL_UART_ENABLE_IT(&huart3,UART_IT_PE);
    __HAL_UART_ENABLE_IT(&huart3,UART_IT_RXNE);//Receive Data register not empty interrupt
    __HAL_UART_ENABLE(&huart3);
	
	print_debug(0, "usart1 init, ret = %d", ret);
}

void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
{
    GPIO_InitTypeDef GPIO_InitStruct;
    __HAL_RCC_GPIOA_CLK_ENABLE();
    if(uartHandle->Instance == USART1)
    {

        /* USART1 clock enable */
        __HAL_RCC_USART1_CLK_ENABLE();

        /**USART1 GPIO Configuration
        PA9     ------> USART1_TX
        PA10     ------> USART1_RX
        */
        GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
		
        hdma_usart1_tx.Instance = DMA2_Stream7;
        hdma_usart1_tx.Init.Channel = DMA_CHANNEL_4;
        hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
        hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_usart1_tx.Init.Mode = DMA_NORMAL;
        hdma_usart1_tx.Init.Priority = DMA_PRIORITY_HIGH;
        hdma_usart1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
        {
          _Error_Handler(__FILE__, __LINE__);
        }
		__HAL_LINKDMA(&huart1,hdmatx,hdma_usart1_tx);
		
        hdma_usart1_rx.Instance = DMA2_Stream5;
        hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
        hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_usart1_rx.Init.Mode = DMA_NORMAL;
        hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
        hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
        {
          _Error_Handler(__FILE__, __LINE__);
        }
        __HAL_LINKDMA(&huart1,hdmarx,hdma_usart1_rx);

    }
    else if(uartHandle->Instance == USART3)
    {
        __HAL_RCC_GPIOC_CLK_ENABLE();
        __HAL_RCC_USART3_CLK_ENABLE();
        GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
        HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
		
        /* USER CODE BEGIN USART3_MspInit 1 */
        /* USART3 DMA Init */
        /* USART3_TX Init */
        hdma_usart3_tx.Instance = DMA1_Stream3;
        hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
        hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
        hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_usart3_tx.Init.Mode = DMA_NORMAL;
        hdma_usart3_tx.Init.Priority = DMA_PRIORITY_HIGH;
        hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
        {
          _Error_Handler(__FILE__, __LINE__);
        }
        __HAL_LINKDMA(&huart3,hdmatx,hdma_usart3_tx);
        /* USART3_RX Init */
        hdma_usart3_rx.Instance = DMA1_Stream1;
        hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
        hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
        hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
        hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
        hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
        hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
        hdma_usart3_rx.Init.Mode = DMA_NORMAL;
        hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
        hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
        if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
        {
          _Error_Handler(__FILE__, __LINE__);
        }
        __HAL_LINKDMA(&huart3,hdmarx,hdma_usart3_rx);
    }
 	if(uartHandle->Instance==UART5)
	{
		/* UART5 clock enable */
		__HAL_RCC_UART5_CLK_ENABLE();

		__HAL_RCC_GPIOC_CLK_ENABLE();
		__HAL_RCC_GPIOD_CLK_ENABLE();
		/**UART5 GPIO Configuration
		PC12     ------> UART5_TX
		PD2     ------> UART5_RX
		*/
		GPIO_InitStruct.Pin = GPIO_PIN_12;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
		GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
		HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
		GPIO_InitStruct.Pin = GPIO_PIN_2;
		GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
		GPIO_InitStruct.Pull = GPIO_PULLUP;
		GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
		GPIO_InitStruct.Alternate = GPIO_AF8_UART5;
		HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
		/* USER CODE BEGIN UART5_MspInit 1 */
		/* USER CODE END UART5_MspInit 1 */
	}
  	if(uartHandle->Instance == USART2)
    {
        __HAL_RCC_GPIOD_CLK_ENABLE();
        __HAL_RCC_USART2_CLK_ENABLE();
        GPIO_InitStruct.Pin = GPIO_PIN_5 | GPIO_PIN_6;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
        HAL_GPIO_Init(GPIOD, &GPIO_InitStruct); 
    }
  	if(uartHandle->Instance == USART6)
    {
        __HAL_RCC_GPIOD_CLK_ENABLE();
        __HAL_RCC_USART6_CLK_ENABLE();
        GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_14;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_PULLUP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
        GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
        HAL_GPIO_Init(GPIOG, &GPIO_InitStruct); 
    }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
{
    if(uartHandle->Instance == USART1)
    {
        /* Peripheral clock disable */
        __HAL_RCC_USART1_CLK_DISABLE();

        /**USART1 GPIO Configuration
        PA9     ------> USART1_TX
        PA10     ------> USART1_RX
        */
        HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
    }
}


/* USER CODE BEGIN 1 */
